/**
 * 
 */
package src.model.gameObject.ability;

import src.model.gameObject.GameObject;
import src.model.gameObject.GameObjectType;
import src.model.gameObject.PlayerGameObject;
import src.model.gameObject.eventBrokers.HealthEventBroker;
import src.model.gameObject.eventBrokers.MeleeEventBroker;
import src.model.gameObject.eventInterfaces.HealthPublisher;
import src.model.gameObject.eventInterfaces.MeleeEventPublisher;
import src.model.gameWorld.ActualMap;
import src.model.gameWorld.PlayerLookupTable;
import src.model.gameWorld.Position;

/**
 * @author Tristan
 *
 */
public class MeleeAttack extends ActiveAbility implements MeleeEventPublisher, HealthPublisher{

	private ActualMap am;
	/**
	 * @param pgo
	 * @param aType
	 */
	public MeleeAttack(PlayerGameObject pgo, PlayerLookupTable playerLookup, ActualMap am) {
		super(pgo, AbilityType.MELEEATTACK, playerLookup);
		this.am = am;
	}

	public AbilityReturnResult execute(Position... position){
		if (am.adjacent(am.findObject(super.gameObject()), position[0])){
			GameObject enemyUnit = am.findObject(position[0], GameObjectType.UNIT);
			if (enemyUnit == null) return AbilityReturnResult.IMPOSSIBLE;
			float strength = super.getAttribute(AbilityAttributes.STRENGTH);
			float damage = (float)Math.random() * (strength/2) + strength/2;
			publishHealthModifier(enemyUnit, damage);
			publishMeleeEvent(super.gameObject());
			return AbilityReturnResult.SUCCESS;
		}
		return AbilityReturnResult.IMPOSSIBLE;
	}
	
	public void publishMeleeEvent(GameObject go) {
		MeleeEventBroker.notifyGameObjectMeleedGameObject(super.gameObject(), go);
	}
	
	public void publishHealthModifier(GameObject go,float modifyAmount) {
		HealthEventBroker.notifyModifyHealth(go, modifyAmount);
	}

	public boolean queueToMission(Position...position){
		if (position.length == 0)  return false;
		super.gameObject().enqueueAbility(this, position);
		return true;
	}
	
	public void destroySub(){
		// TODO: IMPLEMENT!!!!
	}
}
